package cn.stylefeng.guns;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.stylefeng.guns.base.utils.StringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.List;
import java.util.Objects;


/**
 * 模拟点击，动态获取页面信息
 *
 * @Author: Uncle liu
 * @Date: 2019-10-07 15:39
 */
public class JsoupHttpClient {

    public static void main(String[] args) throws Exception {
        leagueData("E:\\乱七八糟\\leisu\\英超.xlsx", "英超");
        leagueData("E:\\乱七八糟\\leisu\\德甲.xlsx", "德甲");
        leagueData("E:\\乱七八糟\\leisu\\法甲.xlsx", "法甲");
        leagueData("E:\\乱七八糟\\leisu\\意甲.xlsx", "意甲");
        leagueData("E:\\乱七八糟\\leisu\\西甲.xlsx", "西甲");
//
//        send("https://live.leisu.com/shujufenxi-3438942");
    }


    public static void leagueData(String filename, String leagueName) throws IOException {
        ExcelReader reader = ExcelUtil.getReader(filename);
        List<List<Object>> datas = reader.read();
        for (int i = 1; i < datas.size(); i++) {
            List<Object> Objs = datas.get(i);
            System.out.println(Objs);
            if (Objs.size() > 6 && StringUtil.isNotNull(Objs.get(6))) {
                continue;
            }
            String date = Objs.get(1).toString();
            if (Integer.valueOf(date.split("-")[0]) >= 8) {
                date = "2019-" + date;
            } else {
                date = "2020-" + date;
            }
            DateTime time = DateUtil.parse(date);
            if (System.currentTimeMillis() < time.getTime()) {
                continue;
            }
            boolean isContinue;
            try {
                isContinue = send(Objs.get(5).toString());
                if (Objs.size() == 7) {
                    Objs.add(6, isContinue ? "是" : "否");
                } else {
                    Objs.add(isContinue ? "是" : "否");
                }
            } catch (Exception e) {
            }
        }
        ExcelUtil.getWriter(filename).setSheet(leagueName).write(datas).autoSizeColumnAll().flush();
    }

    public static boolean send(String url) throws IOException {
        boolean isContinue = false;
        Document doc = Jsoup.connect(String.format(url))
                .ignoreContentType(true)
                .userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)")
                .timeout(5000)
                .get();
        Elements scripts = doc.select("script");
        for (int i = 0; i < scripts.size(); i++) {
            String text = scripts.get(i).toString().replace(" ", "");
            if (text.contains("vargetChild")) {
                String[] vars = text.split("\n");
                for (int j = 0; j < vars.length; j++) {
                    if (vars[j].contains("EVENT=[{")) {
                        String event = vars[j].replace("EVENT=", "").replace(";", "");
                        JSONArray events = JSONObject.parseArray(event);
                        for (int k = 0; k < events.size(); k++) {
                            JSONObject json = events.getJSONObject(k);
                            if (Objects.equals(json.getString("type"), "1") && json.getString("data").contains("任意球")) {
                                isContinue = true;
                            }
                        }
                    }
                }
            }
        }
        return isContinue;
    }

}